package com.woniuxy.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.entity.RbacManager;
import com.woniuxy.mapper.RbacManagerMapper;
import com.woniuxy.service.RbacManagerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author linKe
 * @since 2021-11-25
 */
@Service
public class RbacManagerServiceImpl extends ServiceImpl<RbacManagerMapper, RbacManager> implements RbacManagerService {
    /**
     * 注入mapper持久层对象
     */
    @Resource
    private RbacManagerMapper rbacManagerMapper;

    @Override
    public Page<RbacManager> findAllPage(int currentPage, int size) {
        return rbacManagerMapper.selectPage(new Page<RbacManager>(currentPage,size), null);
    }

    @Override
    public RbacManager findOneById(int id) {
        return rbacManagerMapper.selectById(id);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addManager(RbacManager rbacManager) {
        return rbacManagerMapper.insert(rbacManager);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateManager(RbacManager rbacManager) {
        return rbacManagerMapper.updateById(rbacManager);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteManager(int id) {
        return rbacManagerMapper.deleteById(id);
    }

    @Override
    public List<RbacManager> findAllManager() {
        return rbacManagerMapper.selectList(null);
    }



    @Override
    public Page<RbacManager> findPageSearch(Map<String, String> pageInfo) {
        int page  =  Integer.parseInt(pageInfo.get("page"));
        int limit  =  Integer.parseInt(pageInfo.get("limit"));
        String  managerName = pageInfo.get("managerName");
        String status =pageInfo.get("status");
        LambdaQueryWrapper<RbacManager> wrapper = Wrappers.<RbacManager>lambdaQuery().orderByDesc(RbacManager::getId);

        //当managerName不会空的时候会进入
        if (StrUtil.isNotBlank(managerName)) {
            wrapper.like(RbacManager::getAccount,managerName);
            System.out.println(managerName+"-------"+status);
        }
        //当status不会空的时候会进入
        if (StrUtil.isNotBlank(status)) {
            wrapper.like(RbacManager::getStatus,status);
            System.out.println(managerName+"-------"+status);
        }
        System.out.println(managerName+"-------"+status);
        System.out.println(page+limit);
        return rbacManagerMapper.selectPage(new Page<RbacManager>(page,limit), wrapper);
    }


}
